package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class TravelCommendServiceImpl implements ITravelCommendService {

    //注入2个mapper接口对象
    @Autowired
    private TravelCommendMapper travelCommendMapper ;


    public PageInfo<TravelCommend> query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize(), queryObject.getOrderBy());
        List<TravelCommend> list = travelCommendMapper.selectForList(queryObject);
        return new PageInfo(list);
    }

    public void saveOrUpdate(TravelCommend travelCommend) {
        //这里新增或者修改推荐的游记，需要判断它的推荐类型是不是攻略推荐，如果是攻略推荐的话就将之前在数据库中的其余攻略推荐删除掉
        if(travelCommend.getType()==TravelCommend.TYPE_STRATEGY){
            travelCommendMapper.deleteCommendTypeByStrategy(TravelCommend.TYPE_STRATEGY);//删除表中数据状态为攻略推荐的推荐游记
        }
        if (travelCommend.getId() == null) {
            travelCommendMapper.insert(travelCommend);
        }else{
            travelCommendMapper.updateByPrimaryKey(travelCommend);
        }
    }

    public List<TravelCommend> queryCommendByType(Integer type) {
        Date beginTime = null ;//开始时间
        Date endTime = new Date();//结束时间
        //需要判断这个类型是每周的还是每月的还是攻略推荐的
        if(type==TravelCommend.TYPE_WEEK){
            beginTime = DateUtil.getSevenDaysAgo(endTime);
        }else if(type==TravelCommend.TYPE_MONTH){
            beginTime = DateUtil.getAMonthAgo(endTime);
        }
        return travelCommendMapper.selectCommendByType(type,beginTime,endTime);
    }

}
